<?php
include("general_include.php");
include "checklogin.php";
include "top.php";
include "left.php";
$user_game_id = $_REQUEST['user_game_id'];

if(isset($_POST['submit']))
{
   $Formval = $_POST;
   $game_id = $_POST['game_id'];
   $comment = $_POST['comment'];
   $user_game_id = $_POST['user_game_id'];
   $is_active = $_POST['is_active'] =="Y" ? "Y" : "N";
   $valid=1;
   
   if(empty($user_game_id))
   {
	   $dupchk = "SELECT * FROM ".TABLEPREFIX."_user_game where game_id='$game_id' and user_id='".$_SESSION['user_id']."'";
	   $chkArr = $UserManagerObjAjax->GetRecords('Row',$dupchk);
	   $Numrow = count($chkArr);
	   if($Numrow > 0)
	   {
	     $ermsg ='This game is already added';
		 $valid = 0;  
	   }
	   if($valid ==1)
	   {
		   $Sql= "INSERT INTO ".TABLEPREFIX."_user_game set game_id='$game_id',comment='".addslashes($comment)."',is_active='Y',experience='".addslashes($experience)."',
		   user_type_id='2',user_id='".$_SESSION['user_id']."',date_added='".date("Y-m-d")."'";
		   $UserManagerObjAjax->Execute($Sql);
		   if(!mysql_error())
		   {
			 // header("location:my_game.php");
			  echo "<script>window.location.href='student_game.php'</script>";
		   }
	   } 	   
   }
   else if(!empty($user_game_id))
   {
       
	   $dupchk = "SELECT * FROM ".TABLEPREFIX."_user_game where game_id='$game_id' and user_id='".$_SESSION['user_id']."' and user_game_id!='$user_game_id'";
	   $chkArr = $UserManagerObjAjax->GetRecords('Row',$dupchk);
	   $Numrow = count($chkArr);
	   if($Numrow > 0)
	   {
	     $ermsg ='This game is already added';
		 $valid = 0;  
	   }
	   if($valid ==1)
	   { 
		   $Sql= "UPDATE ".TABLEPREFIX."_user_game set game_id='$game_id',comment='".addslashes($comment)."',is_active='$is_active',experience='".addslashes($experience)."',
		   date_edited='".date("Y-m-d")."' where user_game_id='$user_game_id'";
		   $UserManagerObjAjax->Execute($Sql);
		   if(!mysql_error())
		   {
			  echo "<script>window.location.href='student_game.php'</script>";
		   }
	   }	   
   }	   
   

}

if(!empty($user_game_id))
{
	/* Get Record For Display Starts */	

	$SelectgameSql="SELECT * FROM ".TABLEPREFIX."_user_game WHERE user_game_id='".$user_game_id."'";
	$Formval = $UserManagerObjAjax->GetRecords("Row",$SelectgameSql);
	$Formval['experience'] = stripslashes($Formval['experience']);
    $SubmitButton="Edit";	
	
}
else
{
	$SubmitButton="Add";
}

$gameSql = "SELECT game_id,game_name FROM ".TABLEPREFIX."_game where is_active='Y' ORDER BY game_name";
$GameArr = $UserManagerObjAjax->HtmlOptionArrayCreate($gameSql);


$smarty->assign('ermsg',$ermsg);
$smarty->assign('Formval',$Formval);
$smarty->assign('SubmitButton',$SubmitButton);
$smarty->assign('GameArr',$GameArr);
$smarty->assign('user_game_id',$user_game_id);
$smarty->display("student_game_update.tpl");
include("footer.php");
?>